package pers.zb.cloud.service.upms.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.zb.cloud.service.upms.aop.datasource.DS;
import pers.zb.cloud.service.upms.dao.ClientDetailMapper;
import pers.zb.cloud.service.upms.entity.ClientDetail;
import pers.zb.cloud.service.upms.enums.DataSourceEnum;
import pers.zb.cloud.service.upms.service.ClientDetailService;
import pers.zb.cloud.service.upms.vo.ClientVo;
import pers.zb.cloud.service.upms.vo.Page;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("clientDetailServiceImpl")
public class ClientDetailServiceImpl extends BaseServiceImpl<ClientDetail> implements ClientDetailService {

    @Autowired
    private ClientDetailMapper clientDetailMapper;

    /**
     * @DS 注解表示动态切换到 client 数据源上。
     *      注解中的 value 值请查看 application.yml 中的spring.datasource.list.* 的数据源名称
     */
    @DS(DataSourceEnum.CLIENT)
    @Override
    public ClientDetail getClientDetailByClientId(String clientId) {
        return clientDetailMapper.selectByPrimaryKey(clientId);
    }


    @DS(DataSourceEnum.CLIENT)
    @Override
    public Page<ClientDetail> getClientListForPage(Page pageParam) {
        //所有记录数
        List<ClientDetail> clientAll = clientDetailMapper.selectAll();
        int totalRow = clientAll.size();

        //条件分页查询列表
        List<ClientDetail> clientList = clientDetailMapper.getClientList(pageParam);
        return new Page(clientList, pageParam, totalRow);
    }


    @DS(DataSourceEnum.CLIENT)
    @Transactional
    @Override
    public void updateOrSave(int type,ClientDetail clientDetail) throws Exception{
        if(clientDetail == null){
            throw new Exception("应用对象为null");
        }
        if(StringUtils.isEmpty(clientDetail.getClientId())){
            throw new Exception("clientId不能为空");
        }
        if(StringUtils.isEmpty(clientDetail.getClientSecret())){
            throw new Exception("clientSecret不能为空");
        }
        if(StringUtils.isEmpty(clientDetail.getScope())){
            throw new Exception("scope不能为空");
        }
        if(StringUtils.isEmpty(clientDetail.getAuthorizedGrantTypes())){
            throw new Exception("authorizedGrantTypes不能为空");
        }

        if(type == 1){//更新
            clientDetailMapper.updateByPrimaryKeySelective(clientDetail);
        }else { //添加
            ClientDetail clientDetailObject = clientDetailMapper.selectByPrimaryKey(clientDetail.getClientId());
            if(clientDetailObject != null){//重复存在
                throw new Exception("clientId已经存在");
            }
            clientDetailMapper.insert(clientDetail);
        }
    }

    @DS(DataSourceEnum.CLIENT)
    @Transactional
    @Override
    public void deleteClient(String[] clientIdArrs) throws Exception{
        if(clientIdArrs == null){
            throw new Exception("请选择需要删除的应用");
        }

        Example example = new Example(ClientDetail.class);
        example.createCriteria().andIn("clientId", Arrays.asList(clientIdArrs));
        clientDetailMapper.deleteByExample(example);
        /*for (String id : clientIdArrs) {
            ClientDetail clientDetai = getClientDetailByClientId(id);
            *//*if(clientDetai == null){
                throw new Exception("应用" + clientDetai.getClientId() + "不存在");
            }*//*
            clientDetailMapper.deleteByPrimaryKey(clientDetai.getClientId());
        }*/
    }

    @DS(DataSourceEnum.CLIENT)
    @Override
    public List<ClientVo> getClientListTree() {
        Example example = new Example(ClientDetail.class);
        Example.Criteria criteria = example.createCriteria();
        List<ClientDetail> clientDetailList = clientDetailMapper.selectAll();

        List<ClientVo> clientTreeList = new ArrayList<>();
        if(clientDetailList == null || clientDetailList.size() <= 0){
            return clientTreeList;
        }

        //模拟一个父节点
        ClientVo clientVoParent = new ClientVo();
        clientVoParent.setName("电商业务系统");
        clientVoParent.setState("closed");
        clientVoParent.setId("-1");

        List<ClientVo> clientChildrenVos = new ArrayList<ClientVo>();
        for (ClientDetail clientDetail : clientDetailList) {
            ClientVo clientVo = new ClientVo();
            clientVo.setId(clientDetail.getClientId());
            clientVo.setName(clientDetail.getClientId());
            clientVo.setState("open");
            clientVo.setPid(clientVoParent.getId());
            clientChildrenVos.add(clientVo);
        }
        clientTreeList.add(clientVoParent);
        clientVoParent.setChildren(clientChildrenVos);
        return clientTreeList;
    }

    @DS(DataSourceEnum.CLIENT)
    @Override
    public List<ClientDetail> selectAllClientDetailList() {
        return clientDetailMapper.selectAll();
    }
}
